/**
 * @class   wosh::network::NetworkChannelUdp
 * @brief   wosh::NetworkChannel implementation based on UDP protocol.
 *
 * @see     wosh::NetworkManager
 ****************************************************************************
 * @version $Id: NetworkChannelUdp.h 2584 2010-05-27 16:17:07Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Network_NetworkChannelUdp_H__
 #define __WOSH_Network_NetworkChannelUdp_H__

 #include <core/NetworkChannel.h>

 #include <framework/network/SocketUdp.h>


namespace wosh {
 namespace network {

#define _NetworkChannelUdp_PingHeaderSize	8

class SocketUdp;

class NetworkChannelUdp :	public wosh::NetworkChannel,
							public virtual wosh::network::ISocketUdpListener
 {
	WOSH_CLASSNAME(wosh::network::NetworkChannelUdp)
	WOSH_CLASS_ABSTRACT(false)

	public:
		NetworkChannelUdp( const std::string& name = "" );

		virtual ~NetworkChannelUdp();


/** @name Outgoing Connection methods
 * @{
 ******************************************************************************/
	public:
		virtual WRESULT connect( unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT connect( const std::string& addressport, unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT disconnect( unsigned long timeout = LIMIT_ULONG_MAX );

//@}
/** @name Incoming Connection methods
 * @{
 ******************************************************************************/
	public:
		virtual WRESULT listen( unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT listen( const std::string& addressport, unsigned long timeout = LIMIT_ULONG_MAX );

		virtual WRESULT stopListening( unsigned long timeout = LIMIT_ULONG_MAX );

//@}
/** @name Channel methods
 * @{
 ******************************************************************************/
	public:
		virtual WRESULT sendMessage( const wosh::Message* message );
		virtual WRESULT sendData( const char* data, unsigned long size );

		virtual WRESULT ping( unsigned long timeout = 15000 );

		virtual WRESULT declarateWoshHost();

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		const std::string& getTargetAddressPort() const		{ return this->udpSocket->getAddressPort(); }

		virtual const std::string& getRemoteWoshName() const { return this->remoteWoshName; }

		virtual std::string getProtocol() const;

		virtual bool isListening() const;
		virtual bool isConnected() const;

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		WRESULT setBindingAddressPort( const std::string& address_port );

		void setTargetAddressPort( const std::string& address ) { this->targetAddressPort = address; }

//@}
/** @name ISocketTcpListener
 * @{
 ******************************************************************************/
	public:
		virtual void udpIncomingDatagram( const char * data, int maxSize, const std::string& source, wosh::network::SocketUdp* socket );
//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		WRESULT bindSocketUdp();

		WRESULT pingReply( const std::string& source );
		WRESULT connectReply( const std::string& source );

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		std::string targetAddressPort;
		std::string remoteWoshName;

		bool connectedACK;

		mutable int waitingPing;

		wosh::network::SocketUdp* udpSocket;
		char* msgBufferOut;
		wosh::Mutex msgBufferOutMux;

		char pingRequestHeader[_NetworkChannelUdp_PingHeaderSize];
		char pingResponseHeader[_NetworkChannelUdp_PingHeaderSize];

		bool init();
		void deinit();
//@}

	private:
		NetworkChannelUdp(const NetworkChannelUdp&) : wosh::NetworkChannel() { }
		NetworkChannelUdp& operator=(const NetworkChannelUdp&) { return *this; }


}; // class def

 }; // namespace network
}; // namespace wosh

#endif //__WOSH_Network_NetworkChannelUdp_H__
